package com.bdqn.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.crm.pojo.PageBean;
import com.bdqn.crm.pojo.Pagee;
import com.bdqn.crm.pojo.SysRole;
import com.bdqn.crm.pojo.SysUser;
import com.bdqn.crm.service.SysRoleService;
import com.bdqn.crm.service.SysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysRoleService sysRoleService;

    @RequestMapping(value = "/user/list")
    public String list(HttpSession session, Model model, @RequestParam(value = "usrName", required = false) String usrName,
                       @RequestParam(value = "roleId", required = false) Long roleId,
                       @RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex) {
        //数据回显
        model.addAttribute("usrName", usrName);
        model.addAttribute("roleId", roleId);
        //获取角色集合数据
        List<SysRole> list = sysRoleService.findAllRoles();
        model.addAttribute("roles", list);
        //页面查询过来的条件通过SysUser封装
//        SysUser sysUser= new SysUser();
//        if(usrName!=null){
//            sysUser.setUsrName(usrName);
//        }

        PageBean<SysUser> pageBean = sysUserService.pages(usrName, roleId, pageIndex);
        model.addAttribute("userPager", pageBean);
        //划分分页
        /*Pagee<SysUser> pagee = new Pagee();
        pagee.setTotalCount(sysUserService.getPageListTotal(sysUser));  //获取总记录数设置在分页中
        pagee.setCurrPageNo(pageIndex);  //当前页
        model.addAttribute("userPager",pagee);
        //设置分页信息
        List<SysUser> pageList = sysUserService.getPageList(sysUser,pagee);
        model.addAttribute("user",pageList);*/
        return "user/list";
    }

    /**
     * 新增
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "/user/save")
    public String save(SysUser sysUser) {
        sysUserService.save(sysUser);
        return "redirect:/user/list";
    }

    /**
     * 修改
     * @param 
     * @param model
     * @return
     */
    @RequestMapping(value = "/user/edit")
    @RequiresPermissions(value = "用户编辑")
    public String edit(Long usrId, Model model) {
        SysUser user = sysUserService.getUser(usrId);
        List<SysRole> roles = sysRoleService.findAllRoles();
        model.addAttribute("user",user);
        model.addAttribute("roles", roles);
        return "user/edit";
    }

    /**
     * 删除
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "user/del")
    public Map del(Long usrId){
        sysUserService.delUser(usrId);
        Map map = new HashMap();
        map.put("delResult","true");
        return map;
    }
}
